# Copyright 2024 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Create model endpoints table

Revision ID: 6925effc8fb1
Revises: 22dd9f3bdc66
Create Date: 2025-03-02 21:50:08.179139

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "6925effc8fb1"
down_revision = "22dd9f3bdc66"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "model_endpoints",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("uid", sa.String(length=32), nullable=True),
        sa.Column(
            "name", sa.String(length=255, collation="utf8mb3_bin"), nullable=True
        ),
        sa.Column("endpoint_type", sa.Integer(), nullable=False),
        sa.Column(
            "project", sa.String(length=255, collation="utf8mb3_bin"), nullable=True
        ),
        sa.Column("body", mysql.MEDIUMBLOB(), nullable=True),
        sa.Column("created", mysql.TIMESTAMP(fsp=3), nullable=True),
        sa.Column("updated", mysql.TIMESTAMP(fsp=3), nullable=True),
        sa.Column("function_id", sa.Integer(), nullable=True),
        sa.Column("model_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["function_id"],
            ["functions.id"],
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["model_id"],
            ["artifacts_v2.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("uid"),
    )
    op.create_table(
        "model_endpoints_labels",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column(
            "name", sa.String(length=255, collation="utf8mb3_bin"), nullable=True
        ),
        sa.Column(
            "value", sa.String(length=255, collation="utf8mb3_bin"), nullable=True
        ),
        sa.Column("parent", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(["parent"], ["model_endpoints.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name", "parent", name="_model_endpoints_labels_uc"),
    )
    op.create_index(
        "idx_model_endpoints_labels_name_value",
        "model_endpoints_labels",
        ["name", "value"],
        unique=False,
    )
    op.create_table(
        "model_endpoints_tags",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column(
            "project", sa.String(length=255, collation="utf8mb3_bin"), nullable=True
        ),
        sa.Column(
            "name", sa.String(length=255, collation="utf8mb3_bin"), nullable=True
        ),
        sa.Column("obj_id", sa.Integer(), nullable=True),
        sa.Column(
            "obj_name", sa.String(length=255, collation="utf8mb3_bin"), nullable=True
        ),
        sa.ForeignKeyConstraint(["obj_id"], ["model_endpoints.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "project", "name", "obj_name", name="_model_endpoints_tags_uc"
        ),
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("model_endpoints_tags")
    op.drop_index(
        "idx_model_endpoints_labels_name_value", table_name="model_endpoints_labels"
    )
    op.drop_table("model_endpoints_labels")
    op.drop_table("model_endpoints")
    # ### end Alembic commands ###
